package io.renren.com.qywt.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.com.qywt.dao.LocationFeeDao;
import io.renren.com.qywt.dto.AftersaleFinanceCollectDTO;
import io.renren.com.qywt.dto.ContractDTO;
import io.renren.com.qywt.dto.InventoryInfoDTO;
import io.renren.com.qywt.dto.MaxContractDTO;
import io.renren.com.qywt.entity.AftersaleLocationFeeEntity;
import io.renren.commons.dynamic.datasource.annotation.DataSource;
import io.renren.commons.mybatis.service.impl.BaseServiceImpl;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.enums.DeleteEnum;
import io.renren.commons.tools.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service
public class TaskLocationFeeService extends BaseServiceImpl<LocationFeeDao,InventoryInfoDTO> {
    @Autowired
    private TaskContractService contractService;

    @DataSource("slave1")
    @Transactional
    public List<AftersaleLocationFeeEntity> getInventoryInfoList(UserDetail userDetail,AftersaleFinanceCollectDTO dto){
        Map<String,Object> params=new HashMap<>();
        Date collectDate=new Date();
        if(ObjectUtil.isNull(dto)){
            collectDate=DateUtils.addDateDays(new Date(),-1);
        }else{
            collectDate=dto.getCollectDate();
        }
        String collectDateStr=DateUtils.format(collectDate,DateUtils.DATE_PATTERN);
        params.put("collectDate",collectDateStr);
        List<InventoryInfoDTO> inventoryInfoList=baseDao.getInventoryInfoList(params);
        List<AftersaleLocationFeeEntity> aftersaleLocationFeeEntityList=new ArrayList<>();
        /**
         * 临时变量
         */
        double weightFee=0.0;
        double numFee=0.0;
        double cubeFee=0.0;

        for(InventoryInfoDTO inventoryInfoDTO:inventoryInfoList){
            ContractDTO contractDTO = contractService.getContractBySkuId(inventoryInfoDTO.getSkuId());
            if(ObjectUtil.isNotNull(contractDTO)) {
                if (contractDTO.getNeedMax() == 1) {//需要修正值
                    MaxContractDTO maxContractDTO = contractService.getMaxContractBySkuId(inventoryInfoDTO.getSkuId());
                    if(ObjectUtil.isNotNull(maxContractDTO)){
                        weightFee=inventoryInfoDTO.getOnhandQty()*inventoryInfoDTO.getSkuWeight()*maxContractDTO.getInventoryWeight();
                        numFee=inventoryInfoDTO.getOnhandQty()*maxContractDTO.getInventoryNum();
                        //cubeFee=inventoryInfoDTO.getOnhandQty()*(inventoryInfoDTO.getSkuCube()/1000000)*maxContractDTO.getInventoryCube();
                        cubeFee=inventoryInfoDTO.getOnhandQty()*inventoryInfoDTO.getSkuCube()*maxContractDTO.getInventoryCube();
                        double max=Collections.max(Arrays.asList(weightFee,numFee,cubeFee));
                        if(max==weightFee){//按照重量计算
                            inventoryInfoDTO.setRecordType(2);
                        }else if(max==numFee){//按照数量收费
                            inventoryInfoDTO.setRecordType(1);
                        }else if(max==cubeFee){
                            inventoryInfoDTO.setRecordType(3);
                        }
                        inventoryInfoDTO.setMaxCost(max);
                    }
                }
                //合同仓储费计算方式
                String type= contractDTO.getInventoryUnit();//合同仓储费计算方式
                double loactionFee=0.0;
                // C体积，N件数，D固定天数
                if ("C".equals(type)) {
                    //loactionFee = (inventoryInfoDTO.getSkuCube()/1000000) * inventoryInfoDTO.getOnhandQty()*contractDTO.getInventoryCheck();
                    loactionFee =inventoryInfoDTO.getSkuCube() * inventoryInfoDTO.getOnhandQty()*contractDTO.getInventoryFee();
                }else if ("W".equals(type)) {
                    loactionFee = inventoryInfoDTO.getSkuWeight() * inventoryInfoDTO.getOnhandQty()*contractDTO.getInventoryCheck();
                }else if ("D".equals(type)) {
                    loactionFee = inventoryInfoDTO.getOnhandQty()*contractDTO.getInventoryFee();
                }
                inventoryInfoDTO.setTotalCost(loactionFee);
            }
            //入库费明细
            AftersaleLocationFeeEntity entity= new AftersaleLocationFeeEntity();
            entity.setBatchNo(collectDateStr.replaceAll("-",""));
            entity.setProdSku(inventoryInfoDTO.getSku());
            entity.setProdName(inventoryInfoDTO.getItemname());
            entity.setProdNum(inventoryInfoDTO.getOnhandQty());
            entity.setProdWeight(inventoryInfoDTO.getSkuWeight());
            entity.setProdVolume(inventoryInfoDTO.getSkuCube());
            entity.setLocationFee(BigDecimal.valueOf(inventoryInfoDTO.getTotalCost()).setScale(2,BigDecimal.ROUND_HALF_DOWN));
            entity.setStatus(1);
            entity.setDelFlag(DeleteEnum.NO.value());
            entity.setTenantCode(userDetail.getTenantCode());
            entity.setCreator(userDetail.getId());
            if(entity.getLocationFee().doubleValue()>0){
                aftersaleLocationFeeEntityList.add(entity);
            }
        }
        return aftersaleLocationFeeEntityList;
    }

    @DataSource("slave1")
    @Transactional
    public Integer getByProdCode(String prodCode){
        return baseDao.getByProdCode(prodCode);
    }

}
